﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using CFM.DTO;
using CFM.DAL;

namespace CFM.API
{
    public class UnitAPI
    {
        private DataHelper _helper = null;
        public UnitAPI()
        {
            _helper = new DataHelper();  
        }

        public List<Unit> GetAll()
        {
            string sql = "select * from unit";
            SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
            return _helper.MapDataReaderToObject<Unit>(dr);
        }

        public bool Create(Unit unit)
        {
            string sql = "insert into unit(unitid,unitname) values(@unitid,@unitname)";
            SqlParameter[] prs = 
            {
                new SqlParameter("@unitid",unit.UnitId),
                new SqlParameter("@unitname",unit.UnitName)
            };
            return _helper.ExcuteNonQuery(sql, prs); 
        }

        public bool Update(Unit unit)
        {
            string sql = "update unit set unitname = @unitname where unitid = @unitid";
            SqlParameter[] prs = 
            {
                new SqlParameter("@unitid",unit.UnitId),
                new SqlParameter("@unitname",unit.UnitName)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Delete(string unitId)
        {
            string sql = "delete unit where unitid = @unitid";
            SqlParameter[] prs = 
            {
                new SqlParameter("@unitid",unitId)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }
    }
}
